package com.client.view;

import com.common.Message;
import com.client.controller.ClientSocketThreadManager;
import com.client.controller.ChatFrameManager;
import com.client.controller.FriendListFrameManager;
import com.common.MessageType;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.ObjectOutputStream;
import java.io.OutputStream;

/**
 * @author lxl
 * @date 2020/12/26 - 8:31
 */
/*
我的好友列表
 */
public class FriendListFrame extends JFrame implements ActionListener, MouseListener, WindowListener {
    //测试用例
    /*public static void main(String[] args) {
        new QqFriendList("1");
    }*/
    JFrame parent;
    //处理第一张卡片
    JPanel jphy1,jphy2,jphy3;
    JButton jbhy1, jbhy2, jbhy3;
    JScrollPane jsp1;

    //第二张卡片
    JPanel jpmsr1,jpmsr2,jpmsr3;
    JButton jbmsr1,jbmsr2,jbmsr3;
    JScrollPane jsp2;

    JLabel[] jbls;
    //整个设置成卡片布局
    CardLayout cl;
    public FriendListFrame(JFrame parent, String ownerId){
        this.parent=parent;
        //处理第一章卡片
        jbhy1 = new JButton("行政楼");

        jbhy2 = new JButton("实验楼");
        jbhy2.addActionListener(this);
        jbhy3 = new JButton("宿舍区");
        jphy1 = new JPanel(new BorderLayout());
        //假定有50人
        jphy2 = new JPanel(new GridLayout(51,1,4,4));
        //给jphy2加上50个好友
        jbls = new JLabel[50];
        //下面三行是all窗口
        JLabel allLabel=new JLabel("All",new ImageIcon("images/pig.png"),JLabel.LEFT);
        allLabel.addMouseListener(this);
        jphy2.add(allLabel);
        for (int i =0;i< jbls.length;i++){
            jbls[i]=new JLabel(i+1+"",new ImageIcon("images/panda.png"),JLabel.LEFT);
            jbls[i].setEnabled(false);
            if(jbls[i].getText().equals(ownerId)){
                jbls[i].setEnabled(true);
            }
            jbls[i].addMouseListener(this);
            jphy2.add(jbls[i]);
        }
        jphy3 = new JPanel(new GridLayout(2,1));
        //把其他两个按钮放到jp3中
        jphy3.add(jbhy2);
        jphy3.add(jbhy3);
        //滑动面板加入好友列表
        jsp1 = new JScrollPane(jphy2);

        //设置整个页面, 按钮放最上面
        //滑动面板放中间(滑动面板包含了好友列表)
        //其他两个按钮放在最下面
        //jphy1,是整个panel,包含下两者
        //jphy2,是好友列表
        //jphy3,是其他列表
        jphy1.add(jbhy1,"North");
        jphy1.add(jsp1,"Center");
        jphy1.add(jphy3,"South");

        //处理第二张卡片
        jbmsr1 = new JButton("行政楼");
        jbmsr1.addActionListener(this);
        jbmsr2 = new JButton("实验楼");
        jbmsr3 = new JButton("宿舍区");
        jpmsr1 = new JPanel(new BorderLayout());
        //假定有50人
        jpmsr2 = new JPanel(new GridLayout(20,1,4,4));
        //给jphy2加上20个好友
        JLabel[] jbls2 = new JLabel[20];
        for (int i =0;i< jbls2.length;i++){
            jbls2[i]=new JLabel(i+1+"",new ImageIcon("images/sheep.png"),JLabel.LEFT);
            jpmsr2.add(jbls2[i]);
        }
        jpmsr3 = new JPanel(new GridLayout(2,1));
        //把其他两个按钮放到jp3中
        jpmsr3.add(jbmsr1);
        jpmsr3.add(jbmsr2);
        //滑动面板加入好友列表
        jsp2 = new JScrollPane(jpmsr2);

        jpmsr1.add(jpmsr3,"North");
        jpmsr1.add(jsp2,"Center");
        jpmsr1.add(jbmsr3,"South");

        //整体布局
        cl=new CardLayout();
        this.setLayout(cl);
        this.add(jphy1,"1");
        this.add(jpmsr1,"2");
        this.setSize(225,400);
        this.setTitle(ownerId);
        this.setLocationRelativeTo(parent);
        this.addWindowListener(this);//设置关闭事件
        //this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        //点击了陌生人按钮,就显示第二章卡片
        if (e.getSource()==jbhy2){
            cl.show(this.getContentPane(),"2");
        }
        if (e.getSource()==jbmsr1){
            cl.show(this.getContentPane(),"1");
        }
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        //响应用户双击事件,得到好友编号
        if(e.getClickCount()==2){
            JLabel temp = (JLabel)e.getSource();
            if(!temp.isEnabled()){ return; }
            String friendNo= ((JLabel)e.getSource()).getText();
            if(friendNo.equals(this.getTitle())){ return; }
            ChatFrame chatFrame = ChatFrameManager.getQqChat(this.getTitle()+"And"+friendNo);
            if(chatFrame ==null){
                chatFrame = new ChatFrame(this,this.getTitle(), friendNo);
                ChatFrameManager.addQqChat(this.getTitle()+"And"+friendNo, chatFrame);
            }
            chatFrame.toFront();
            chatFrame.setVisible(true);
        }
    }

    @Override
    public void mousePressed(MouseEvent e) { }

    @Override
    public void mouseReleased(MouseEvent e) { }

    @Override
    public void mouseEntered(MouseEvent e) {
        JLabel jl = (JLabel)e.getSource();
        jl.setForeground(Color.red);
    }

    @Override
    public void mouseExited(MouseEvent e) {
        JLabel jl = (JLabel)e.getSource();
        jl.setForeground(Color.black);
    }
    //更新在线好友情况
    public void updateFriend(Message message){
        for (int i = 0;i< jbls.length;i++){
            jbls[i].setEnabled(false);
        }
        String[] onlineFriends= message.getText().split(" ") ;
        for (int i = 0;i<onlineFriends.length;i++){
            jbls[Integer.parseInt(onlineFriends[i])-1].setEnabled(true);
        }
    }


    @Override
    public void windowOpened(WindowEvent e) { }

    @Override
    public void windowClosing(WindowEvent e) {
        int option = JOptionPane.showConfirmDialog(this, "确定退出系统?", "提示",
            JOptionPane.YES_NO_OPTION);
        if (option == JOptionPane.YES_OPTION)
        {
            if (e.getWindow() == this) {
                try{
                    OutputStream os = ClientSocketThreadManager.getClientConServerThread(this.getTitle()).getS().getOutputStream();
                    ObjectOutputStream oos= new ObjectOutputStream(os);
                    Message message = new Message();
                    message.setMesType(MessageType.message_close);
                    message.setSender(this.getTitle());
                    oos.writeObject(message);
                    ClientSocketThreadManager.getClientConServerThread(this.getTitle()).flag = 0;
                    ClientSocketThreadManager.getClientConServerThread(this.getTitle()).getS().close();
                    ClientSocketThreadManager.remove(this.getTitle());
                    ChatFrameManager.remove(this.getTitle());
                    FriendListFrameManager.remove(this.getTitle());
                }catch (Exception arg0){
                    arg0.printStackTrace();
                }
                this.dispose();
                System.exit(0);System.exit(0);
            } else {
                return;
            }
        }
        else if(option == JOptionPane.NO_OPTION){
            if (e.getWindow() == this) {
                return;
            }
        } }

    @Override
    public void windowClosed(WindowEvent e) { }

    @Override
    public void windowIconified(WindowEvent e) { }

    @Override
    public void windowDeiconified(WindowEvent e) { }

    @Override
    public void windowActivated(WindowEvent e) { }

    @Override
    public void windowDeactivated(WindowEvent e) { }

    public void disposeAll() {
        this.dispose();
        System.exit(0);
    }
}
